package com.md.youyouhui.ui.user;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityUserCouponBinding;
import com.md.youyouhui.databinding.AdapterUserCouponBinding;
import com.md.youyouhui.net.response.CouponResponse;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.ui.adapter.DataBoundViewHolder;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by AdministratorZJ
 * on 2019/4/12.
 */
public class CouponUserActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    private ActivityUserCouponBinding couponBinding;
    private MyAdapter adapter;
    private MainViewModel viewModel;

    @Override
    public void setContentLayout() {
        couponBinding = DataBindingUtil.setContentView(this, R.layout.activity_user_coupon);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        setSupportActionBar(couponBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void initData() {
        adapter = new MyAdapter();
        couponBinding.couponRecycler.setAdapter(adapter);
        couponBinding.couponGroup.setOnCheckedChangeListener(this);
        getCoupon(0);
    }

    private void getCoupon(int type) {
        //0未使用，1已使用，2已过期0 0
        viewModel.getCoupon(1, 100, type).observe(this, couponResponse -> {
            assert couponResponse != null;
            Log.e("couponResponse",couponResponse.toString());
            switch (couponResponse.status) {

                case LOADING:
                    couponBinding.progress.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    couponBinding.progress.setVisibility(View.GONE);
                    CouponResponse data = couponResponse.data;

                    if (null == data) {
                        return;
                    }
                    List<CouponResponse.Rows> rows = data.rows;
                    if (rows.size() == 0) {
                        Toast.makeText(ct, "暂无优惠券", Toast.LENGTH_SHORT).show();
                    }
                    adapter.upDate(type, rows);
                    break;
                case ERROR:
                    ToastUtils.errMake(this, couponResponse.errorCode);
                    couponBinding.progress.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.couponSure:
                getCoupon(0);
                break;
            case R.id.couponYiUse:
                getCoupon(1);
                break;
            case R.id.couponExpired:
                getCoupon(2);
                break;
            default:
                break;
        }
    }

    class MyAdapter extends RecyclerView.Adapter<DataBoundViewHolder<AdapterUserCouponBinding>> {
        private int type = 0;
        List<CouponResponse.Rows> rows;

        public void upDate(int type, List<CouponResponse.Rows> rows) {
            this.type = type;
            this.rows = rows;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public DataBoundViewHolder<AdapterUserCouponBinding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            AdapterUserCouponBinding userBinding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.adapter_user_coupon, viewGroup, false);
            return new DataBoundViewHolder<>(userBinding);
        }

        @Override
        public void onBindViewHolder(@NonNull DataBoundViewHolder<AdapterUserCouponBinding> holder, int i) {
            if (type == 0) {
                holder.binding.couponBg.setImageResource(R.drawable.img_youhuiquan_not_uesd);
                holder.binding.couponPrice.setTextColor(getResources().getColor(R.color.coupon_price_color));
                holder.binding.couponContent.setTextColor(getResources().getColor(R.color.coupon_content_color));
            } else if (type == 1) {
                holder.binding.couponBg.setImageResource(R.drawable.img_youhuiquan_used);
                holder.binding.couponPrice.setTextColor(getResources().getColor(R.color.main_bg));
                holder.binding.couponContent.setTextColor(getResources().getColor(R.color.main_bg));
            } else if (type == 2) {
                holder.binding.couponBg.setImageResource(R.drawable.img_youhuiquan_out_of_ues);
                holder.binding.couponPrice.setTextColor(getResources().getColor(R.color.main_bg));
                holder.binding.couponContent.setTextColor(getResources().getColor(R.color.main_bg));
            }
            //useType 0有门槛,1无门槛
            holder.binding.couponPrice.setText(rows.get(i).amount);
            holder.binding.couponContent.setText(rows.get(i).useType);
           // Log.e("couponData",rows.get(i).createTime.getTime()+"");
             holder.binding.couponDay.setText("有效期至"+numberDateFormat(rows.get(i).overDueTime,"yyyy-MM-dd"));


            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (type == 0) {
                        // Toast.makeText(ct, "优惠券", Toast.LENGTH_SHORT).show();
                    }
                }
            });

        }
        public  String numberDateFormat(String timestamp,String  simpleDateFormatType){
            SimpleDateFormat sdf = new SimpleDateFormat(simpleDateFormatType);//要转换的时间格式
            String date = null;
            if (timestamp.length() == 13){
                date = sdf.format(Long.parseLong(timestamp));
            }else{
                date = sdf.format(Long.parseLong(timestamp)*1000);
            }
            return date;
        }

        @Override
        public int getItemCount() {
            return null == rows ? 0 : rows.size();
        }
    }
}
